﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class SalesOrderShow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["SalesOrderID"] != null)
            {
                if (mk.IsNumber(Request.QueryString["SalesOrderID"].ToString()))
                {
                    //用一个隐藏Label保存传递过来的ID
                    this.lblSalesOrderID.Text = Request.QueryString["SalesOrderID"].ToString();
                }
                else
                {
                    mk.Alert("参数错误！");
                }
            }
            else
            {
                mk.Alert("参数错误！");
            }
            this.BindData();
        }
        private void BindData()
        {
            //给日期输入框添加只读属性，不能在属性里直接设置，不然会取不到值
            this.txtDateDelivery.Attributes["readonly"] = "true";
            this.txtDateInspect.Attributes["readonly"] = "true";
            this.txtDateInstall.Attributes["readonly"] = "true";
            this.txtDateOrder.Attributes["readonly"] = "true";

            var bll =mk.l.Sales_SalesOrderHeader.ToList();
                   Sales_SalesOrderHeader m = bll.Where(h=>h.SalesOrderID==int.Parse(this.lblSalesOrderID.Text)).FirstOrDefault();
            if (m != null)
            {
                //不能修改已经结账完毕的订单

                //先判断是否是本人添加的订单，不能修改别人添加的订单
                //UART.Accounts.Bus.User currentUser = (UART.Accounts.Bus.User)(Session["UserInfo"]);
                //if (m.Designer == currentUser.UserName)
                //{
                //设置经办人
               Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                 this.lblRootDepartmentID.Text = bllDept.Where(j=>j.ClassID==(int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                if (int.Parse(this.lblRootDepartmentID.Text) == m.RootDepartmentID)
                {
                    this.lblCustomerID.Text = m.CustomerID.ToString();
                    //显示客户信息
                  var bllC = mk.l.Sales_Customer.ToList();
           Sales_Customer mC = bllC.Where(h=>h.CustomerID==int.Parse(this.lblCustomerID.Text)).FirstOrDefault();
                    this.txtCustomerName.Text = mC.CustomerName;
                    this.txtCellPhone.Text = mC.CellPhone;
                    this.txtPhone.Text = mC.Phone;

                    this.txtAddress.Text = m.Address;
                    this.txtDateDelivery.Date = m.DateDelivery;
                    this.txtDateInspect.Date = m.DateInspect;
                    this.txtDateInstall.Date = m.DateInstall;
                    this.txtDateOrder.Date = m.DateOrder;
                    this.txtSalesOrderNumber.Text = m.SalesOrderNumber;

                    this.txtNotesInstall.Text = m.NotesInstall;
                    this.txtNotesManufacture.Text = m.NotesManufacture;
                    this.txtNotesOrder.Text = m.NotesOrder;
                    this.lblContractAmount.Text = m.ContractAmount.ToString();
                    this.lblTotalDue.Text = m.TotalDue.ToString();
                    this.lblSubTotal.Text = m.SubTotal.ToString();
                    this.lblBrokerage.Text = m.Brokerage.ToString();
                    this.lblDesignerUserName.Text = m.Designer;
                var bllUser = mk.l.Accounts_Users.ToList();
                Accounts_Users mUser = bllUser.Where(h=>h.UserName==m.Designer).FirstOrDefault();
                    this.lblDesignerTrueName.Text = mUser.TrueName;

                    //if (m.Status != "1")
                    //{
                    //    this.btnOK.Enabled = false;
                    //    this.btnOK.Visible = false;
                    //}
                    //}
                    //else
                    //{
                    //    UART.Common.MsgBox("不能修改他人添加的订单！", "SalesOrderList.aspx");
                    //}
                }
                else
                {
                   mk.Alert("只能查看本店的订单！");
                }
            }
            else
            {
               mk.Alert("参数错误！");
            }

        }

    }
}
